import java.util.ArrayList;
import java.util.List;
import java.util.Random;

public class CardDemo {
    public static final String[] suits = {"♣","♠","♦","♥"};
    private static void swarp(List<Card> list,int currentIndex,int randomIndex) {
        Card tmp = list.get(currentIndex);
        list.set(currentIndex,list.get(randomIndex));
        list.set(randomIndex,tmp);
    }

    public static void main(String[] args) {
        Card c = new Card();
        List<Card> cards = new ArrayList<>();
        //生成52张牌
        for (int i = 1; i <= 13; i++) {
            for (int j = 0; j < suits.length; j++) {
                c = new Card(suits[j],i);
                cards.add(c);
            }
        }
        System.out.println("洗牌前：");
        int count = 0;
        for (int i = 0; i < cards.size(); i++) {
            System.out.print(cards.get(i));
            count++;
            if (count % 8 == 0) {
                System.out.println();
            }
        }
        System.out.println();
        //洗牌
        Random r = new Random();
        for (int i = cards.size()-1; i > 0 ; i--) {
            //randomIndex的范围是0~i-1;
            int randomIndex = r.nextInt(i);
            swarp(cards,i,randomIndex);
        }
        System.out.println("洗牌后：");
        count = 0;
        for (int i = 0; i < cards.size(); i++) {
            System.out.print(cards.get(i));
            count++;
            if (count % 8 == 0) {
                System.out.println();
            }
        }
        System.out.println();
        //3人抽牌,每轮每人抽一张，抽5轮
        //二维数组
        List<Card> hand0 = new ArrayList<>();
        List<Card> hand1 = new ArrayList<>();
        List<Card> hand2 = new ArrayList<>();
        List<List<Card>> players = new ArrayList<>();
        players.add(hand0);
        players.add(hand1);
        players.add(hand2);
        for (int i = 0; i < 5; i++) {
            for (int j = 0; j < players.size(); j++) {
                //每次都从牌的第一张开始抽，抽完了，第二张就变成了第一张
                Card select = cards.remove(0);
                players.get(j).add(select);
            }
        }

        for (int i = 0; i < players.size(); i++) {
            System.out.println("第"+(i+1)+"个人的牌:"+players.get(i));
        }
        System.out.println("剩余的牌:");
        count = 0;
        for (int i = 0; i < cards.size(); i++) {
            System.out.print(cards.get(i));
            count++;
            if (count % 8 == 0) {
                System.out.println();
            }
        }
    }
}
